<?php

namespace Kunfeng\Dify\Callback;

use Kunfeng\Dify\Event\WorkflowStartedEvent;
use Kunfeng\Dify\Event\WorkflowFinishedEvent;
use Kunfeng\Dify\Event\NodeStartedEvent;
use Kunfeng\Dify\Event\NodeFinishedEvent;
use Kunfeng\Dify\Event\IterationStartedEvent;
use Kunfeng\Dify\Event\IterationNextEvent;
use Kunfeng\Dify\Event\IterationCompletedEvent;
use Kunfeng\Dify\Event\LoopStartedEvent;
use Kunfeng\Dify\Event\LoopNextEvent;
use Kunfeng\Dify\Event\LoopCompletedEvent;
use Kunfeng\Dify\Event\WorkflowTextChunkEvent;
use Kunfeng\Dify\Event\ErrorEvent;
use Kunfeng\Dify\Event\PingEvent;

/**
 * 工作流流式回调处理器
 */
class WorkflowStreamCallback
{
    private $callbacks;

    public function __construct(array $callbacks = [])
    {
        $this->callbacks = $callbacks;
    }

    /**
     * 处理工作流开始事件
     */
    public function handleWorkflowStarted(WorkflowStartedEvent $event): void
    {
        if (isset($this->callbacks['onWorkflowStarted'])) {
            call_user_func($this->callbacks['onWorkflowStarted'], $event);
        }
    }

    /**
     * 处理工作流完成事件
     */
    public function handleWorkflowFinished(WorkflowFinishedEvent $event): void
    {
        if (isset($this->callbacks['onWorkflowFinished'])) {
            call_user_func($this->callbacks['onWorkflowFinished'], $event);
        }
    }

    /**
     * 处理节点开始事件
     */
    public function handleNodeStarted(NodeStartedEvent $event): void
    {
        if (isset($this->callbacks['onNodeStarted'])) {
            call_user_func($this->callbacks['onNodeStarted'], $event);
        }
    }

    /**
     * 处理节点完成事件
     */
    public function handleNodeFinished(NodeFinishedEvent $event): void
    {
        if (isset($this->callbacks['onNodeFinished'])) {
            call_user_func($this->callbacks['onNodeFinished'], $event);
        }
    }

    /**
     * 处理迭代开始事件
     */
    public function handleIterationStarted(IterationStartedEvent $event): void
    {
        if (isset($this->callbacks['onIterationStarted'])) {
            call_user_func($this->callbacks['onIterationStarted'], $event);
        }
    }

    /**
     * 处理迭代下一步事件
     */
    public function handleIterationNext(IterationNextEvent $event): void
    {
        if (isset($this->callbacks['onIterationNext'])) {
            call_user_func($this->callbacks['onIterationNext'], $event);
        }
    }

    /**
     * 处理迭代完成事件
     */
    public function handleIterationCompleted(IterationCompletedEvent $event): void
    {
        if (isset($this->callbacks['onIterationCompleted'])) {
            call_user_func($this->callbacks['onIterationCompleted'], $event);
        }
    }

    /**
     * 处理循环开始事件
     */
    public function handleLoopStarted(LoopStartedEvent $event): void
    {
        if (isset($this->callbacks['onLoopStarted'])) {
            call_user_func($this->callbacks['onLoopStarted'], $event);
        }
    }

    /**
     * 处理循环下一步事件
     */
    public function handleLoopNext(LoopNextEvent $event): void
    {
        if (isset($this->callbacks['onLoopNext'])) {
            call_user_func($this->callbacks['onLoopNext'], $event);
        }
    }

    /**
     * 处理循环完成事件
     */
    public function handleLoopCompleted(LoopCompletedEvent $event): void
    {
        if (isset($this->callbacks['onLoopCompleted'])) {
            call_user_func($this->callbacks['onLoopCompleted'], $event);
        }
    }

    /**
     * 处理工作流文本块事件
     */
    public function handleWorkflowTextChunk(WorkflowTextChunkEvent $event): void
    {
        if (isset($this->callbacks['onWorkflowTextChunk'])) {
            call_user_func($this->callbacks['onWorkflowTextChunk'], $event);
        }
    }

    /**
     * 处理错误事件
     */
    public function handleError(ErrorEvent $event): void
    {
        if (isset($this->callbacks['onError'])) {
            call_user_func($this->callbacks['onError'], $event);
        }
    }

    /**
     * 处理 Ping 事件
     */
    public function handlePing(PingEvent $event): void
    {
        if (isset($this->callbacks['onPing'])) {
            call_user_func($this->callbacks['onPing'], $event);
        }
    }

    /**
     * 处理异常
     */
    public function handleException(\Throwable $exception): void
    {
        if (isset($this->callbacks['onException'])) {
            call_user_func($this->callbacks['onException'], $exception);
        }
    }
}